{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from llama_index.llms.perplexity import Perplexity\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "pplx_api_key = os.getenv(\"PERPLEXITY_API_KEY\")\n",
    "\n",
    "llm = Perplexity(\n",
    "    api_key=pplx_api_key, model=\"mistral-7b-instruct\", temperature=0.5\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assistant: Harry Potter is a series of seven fantasy novels written by British author J.K. Rowling. The books follow Harry Potter, an orphaned boy who discovers he is a wizard, as he attends Hogwarts School of Witchcraft and Wizardry and learns to master his magical abilities. Throughout the series, Harry faces numerous challenges and battles against the dark wizard Lord Voldemort. The novels are known for their intricate plotting, richly detailed world-building, and complex characters. The Harry Potter series has become a cultural phenomenon and has been translated into over 80 languages. The first novel, \"Harry Potter and the Philosopher's Stone,\" was published in 1997, and the last book, \"Harry Potter and the Deathly Hallows,\" was published in 2007. The series has also been adapted into a successful film series.\n"
     ]
    }
   ],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "messages_dict = [\n",
    "    {\"role\": \"system\", \"content\": \"Be precise and concise.\"},\n",
    "    {\"role\": \"user\", \"content\": \"tell me about harry potter\"},\n",
    "]\n",
    "messages = [ChatMessage(**msg) for msg in messages_dict]\n",
    "\n",
    "external_response = llm.chat(messages)\n",
    "print(external_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assistant: Harry Potter is a popular series of seven fantasy novels written by British author J.K. Rowling. The series follows the adventures of the titular character, Harry Potter, and his friends Ron Weasley and Hermione Granger, as they attend Hogwarts School of Witchcraft and Wizardry and battle against the dark wizard, Lord Voldemort. The series explores themes of love, friendship, bravery, and the power of choice. It has also been adapted into a successful film franchise and stage play.\n"
     ]
    }
   ],
   "source": [
    "llm = Perplexity(\n",
    "    api_key=pplx_api_key, model=\"mixtral-8x7b-instruct\", temperature=0.5\n",
    ")\n",
    "messages_dict = [\n",
    "    {\"role\": \"system\", \"content\": \"Be precise and concise.\"},\n",
    "    {\"role\": \"user\", \"content\": \"tell me about harry potter\"},\n",
    "]\n",
    "messages = [ChatMessage(**msg) for msg in messages_dict]\n",
    "\n",
    "external_response = llm.chat(messages)\n",
    "print(external_response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "assistant: Harry Potter is a series of seven fantasy novels written by British author J.K. Rowling, published between 1997 and 2007. The books follow the adventures of a young wizard named Harry Potter as he attends Hogwarts School of Witchcraft and Wizardry and battles against the evil Lord Voldemort.\n"
     ]
    }
   ],
   "source": [
    "llm = Perplexity(\n",
    "    api_key=pplx_api_key, model=\"sonar-small-chat\", temperature=0.5\n",
    ")\n",
    "messages_dict = [\n",
    "    {\"role\": \"system\", \"content\": \"Be precise and concise.\"},\n",
    "    {\"role\": \"user\", \"content\": \"tell me about harry potter\"},\n",
    "]\n",
    "messages = [ChatMessage(**msg) for msg in messages_dict]\n",
    "\n",
    "external_response = llm.chat(messages)\n",
    "print(external_response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
